// pages/mycode/mycode.js
import urls from '../../utils/urls.js'
import loginFn from '../../utils/login.js'
var app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		scene: '',
		path: 'pages/index/index'
	},
	downloadphoto: function(event) {
		var that = this;
		var url = this.data.codeImg.file;
		//获取相册授权
		wx.getSetting({
			success(res) {
				if (!res.authSetting['scope.writePhotosAlbum']) {
					wx.authorize({
						scope: 'scope.writePhotosAlbum',
						success() { //这里是用户同意授权后的回调
							that.saveImgToPhotosAlbumTap(url);
						},
						fail() { //这里是用户拒绝授权后的回调
							that.setData({
								saveImgBtnHidden: true,
								openSettingBtnHidden: false
							})
						}
					})
				} else { //用户已经授权过了
					that.saveImgToPhotosAlbumTap(url);
				}
			}
		})
	},
	handleSetting: function(e) {
		let that = this;
		// 对用户的设置进行判断，如果没有授权，即使用户返回到保存页面，显示的也是“去授权”按钮；同意授权之后才显示保存按钮
		if (!e.detail.authSetting['scope.writePhotosAlbum']) {
			wx.showModal({
				title: '警告',
				content: '若不打开授权，则无法将图片保存在相册中！',
				showCancel: false
			})
			that.setData({
				saveImgBtnHidden: true,
				openSettingBtnHidden: false
			})
		} else {
			wx.showModal({
				title: '提示',
				content: '您已授权，赶紧将图片保存在相册中吧！',
				showCancel: false
			})
			that.setData({
				saveImgBtnHidden: false,
				openSettingBtnHidden: true
			})
		}
	},
	saveImgToPhotosAlbumTap: function(e) {
		console.log("ssssssssss")
		console.log('url：' + e)
		wx.showToast({
			title: '保存中...',
			icon: 'success',
			duration: 1000
		})
		wx.downloadFile({
			url: e,
			success: function(res) {
				console.log(res)
				wx.saveImageToPhotosAlbum({
					filePath: res.tempFilePath,
					success: function(res) {
						console.log(res)
						wx.showToast({
							title: '保存成功',
							icon: 'success',
							duration: 1000
						})
					},
					fail: function(res) {
						console.log(res)
						wx.showToast({
							title: '保存失败',
							icon: 'none',
							duration: 1000
						})
					}
				})
			},
			fail: function() {
				console.log('fail')
				wx.showToast({
					title: '保存失败',
					icon: 'none',
					duration: 2000
				})
			}
		})

	},

	getQrcode: function(options) {
		let that = this
		let qrCreate = loginFn()
		wx.request({
			url: urls.qrCreate,
			data: {
				page: that.data.path,
				scene: that.data.scene,
				width: 1280
			},
			methods: 'GET',
			success: function(res) {
				console.log(res, 'code')
				res.data.data.file = urls.zhaopeixun + res.data.data.file
				that.setData({
					codeImg: res.data.data
				})
			},
			fail: function() {},
			complete: options.complete || function() {}
		})
	},

	//  * 生命周期函数--监听页面加载
	//  */
	onLoad: function(options) {
		// const scene = decodeURIComponent(query.scene)
		console.log('options:',options);
		var pages = getCurrentPages()
		var prepages = pages[pages.length - 2]
		var type = options.type;
		var path = 'pages/index/index';
		var scene = 'appId_123';

		if(type == 'school') {
			path = 'pages/institufirst/index';
			scene = 's='+prepages.data.schoolId;
		} else if(type == 'teacher') {
			path = 'pages/teacherpage/teacherpage';
			scene = 't='+prepages.data.mineInfo.teacherId;
		}
		this.setData({
			path: path,
			scene: scene
		});

		this.getQrcode(options)
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
